package org.wyn.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.bson.types.ObjectId;
import org.wyn.common.ResultType;
import org.wyn.mongodb.bean.Blog;
import org.wyn.mongodb.bean.Comment;
import org.wyn.mongodb.bean.User;
import org.wyn.mongodb.dao.impl.BlogDao;
import org.wyn.service.IBlogService;

import com.google.inject.Inject;

/**
 * 日志
 * @author wyn
 *
 */
@Namespace("/blog")
@ParentPackage("security")
public class BlogAction extends WynActionSupport{
	
	/**
	 * 日志列表页面
	 * @return
	 */
	@Action(value="goBlog",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "blog.ftl")})
	public String goBlog(){
		User user = getLoginUser();
		blogs = blogDao.createQuery().filter("authorUserName", user.getUserName()).order("-createTime").asList();
		return SUCCESS;
	}
	/**
	 * 别人的日志列表页面
	 * @return
	 */
	@Action(value="go_other_blog",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "other_blog.ftl")})
	public String goOtherBlog(){
		blogs = blogDao.createQuery().filter("authorUserName", otherUserName).order("-createTime").asList();
		return SUCCESS;
	}
	/**
	 * 日志明细页面
	 * @return
	 */
	@Action(value="blog_detail",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "blog_detail.ftl")})
	public String blogDetail(){
		Blog b = blogDao.findOne("blogId", new ObjectId(blogId));
		getRequest().setAttribute("blog", b);
		return SUCCESS;
	}
	/**
	 * 别人日志明细页面
	 * @return
	 */
	@Action(value="other_blog_detail",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "other_blog_detail.ftl")})
	public String otherBlogDetail(){
		Blog b = blogDao.findOne("blogId", new ObjectId(blogId));
		getRequest().setAttribute("blog", b);
		return SUCCESS;
	}
	/**
	 * 进入写日志页面
	 * @return
	 */
	@Action(value="blog_write",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "blog_write.ftl")})
	public String blogWrite(){
		return SUCCESS;
	}
	/**
	 *写日志保存
	 * @return
	 */
	@Action(value="blog_write_save",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "blog.ftl")})
	public String blogWriteSave(){
		User user = getLoginUser();
		//保存blog
		iBlogService.writeBlog(blog, user);
		blogs = blogDao.createQuery().filter("authorUserName", user.getUserName()).order("-createTime").asList();
		return SUCCESS;
	}
	@Action(value="add_comment",results = {@Result(name = SUCCESS, type = ResultType.FREEMARKER, location = "comment.ftl")})
	public String addComment(){
		comment = iBlogService.addComment(blogId, commentContent, getLoginUser().getUserName());
		return SUCCESS;
	}
	@Inject
	private BlogDao blogDao;
	@Inject
	private IBlogService iBlogService;
	private String blogId;
	private Blog blog;
	private List<Blog> blogs = null;
	private String otherUserName;
	private String commentContent;
	private Comment comment;
	public Comment getComment() {
		return comment;
	}
	public void setComment(Comment comment) {
		this.comment = comment;
	}
	private static final long serialVersionUID = 1L;
	public IBlogService getiBlogService() {
		return iBlogService;
	}
	public void setiBlogService(IBlogService iBlogService) {
		this.iBlogService = iBlogService;
	}
	public String getCommentContent() {
		return commentContent;
	}
	public void setCommentContent(String commentContent) {
		this.commentContent = commentContent;
	}
	public List<Blog> getBlogs() {
		return blogs;
	}
	public void setBlogs(List<Blog> blogs) {
		this.blogs = blogs;
	}
	public BlogDao getBlogDao() {
		return blogDao;
	}
	public void setBlogDao(BlogDao blogDao) {
		this.blogDao = blogDao;
	}
	public String getBlogId() {
		return blogId;
	}
	public void setBlogId(String blogId) {
		this.blogId = blogId;
	}
	public Blog getBlog() {
		return blog;
	}
	public void setBlog(Blog blog) {
		this.blog = blog;
	}
	public String getOtherUserName() {
		return otherUserName;
	}
	public void setOtherUserName(String otherUserName) {
		this.otherUserName = otherUserName;
	}

}
